/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.datalayer.daoimpl;

import cz.muni.fi.diplomka.datalayer.dao.AllianceMemberDao;
import cz.muni.fi.diplomka.datalayer.entity.AllianceMemberEntity;
import cz.muni.fi.diplomka.datalayer.util.DbConnector;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jakub Kutil
 */
public class AllianceMemberDaoImpl implements AllianceMemberDao {

    private Connection con = null;
    private PreparedStatement ps = null;
    private ResultSet rs = null;

    public AllianceMemberDaoImpl() {
    }

    @Override
    public void addAllianceMember(AllianceMemberEntity allianceMemberEntity) {
        if (allianceMemberEntity == null) {
            throw new IllegalArgumentException("AllianceMemberEntity can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String insert = "INSERT INTO alliance_member"
                    + "(player_id, alliance_id) VALUES"
                    + "(?,?)";
            ps = con.prepareStatement(insert);

            ps.setLong(1, allianceMemberEntity.getPlayerId());
            ps.setLong(2, allianceMemberEntity.getAllianceId());

            ps.executeUpdate();

        } catch (SQLException ex) {
            Logger.getLogger(AllianceMemberDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public void deleteAllianceMember(Long allianceMemberId) {
        if (allianceMemberId == null) {
            throw new IllegalArgumentException("AllianceMemberId can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String delete = "DELETE FROM alliance_member WHERE player_id=?";

            ps = con.prepareStatement(delete);

            ps.setLong(1, allianceMemberId);

            ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(AllianceMemberDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public List<AllianceMemberEntity> getAllAllianceMembers(Long allianceId) {
        if (allianceId == null) {
            throw new IllegalArgumentException("Alliance ID cant be null");
        }
        List<AllianceMemberEntity> allianceMemberList = new ArrayList<AllianceMemberEntity>();

        try {
            con = DbConnector.getDBConnection();
            String getAllAllianceMembers = "SELECT * FROM alliance_member WHERE alliance_id=?";

            ps = con.prepareStatement(getAllAllianceMembers);

            ps.setLong(1, allianceId);

            rs = ps.executeQuery();

            while (rs.next()) {
                AllianceMemberEntity allianceMemberEntity = new AllianceMemberEntity();
                allianceMemberEntity.setPlayerId(rs.getLong("player_id"));
                allianceMemberEntity.setAllianceId(rs.getLong("alliance_id"));

                allianceMemberList.add(allianceMemberEntity);

            }
        } catch (SQLException ex) {
            Logger.getLogger(AllianceMemberDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return allianceMemberList;
    }
}
